<?php
	/**
	* Categories View Controller
	* The URL prototype
	* view/name/X|y|Z/                     :  Categories view
	* 
	* Input params: $cat_id and $cat_seo_name;
	* 
	* @author trungdt
	* @since Oct 9, 2011
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');
	
	$cat = null;
	
	if ($cat_id != "")
		$cat=productsmodule::__GetCategoryByID(intval($cat_id));
	else
		$cat=productsmodule::__GetCategoryBySeoName($cat_seo_name);
	
	// determine: if this cat has parent_id == 0 or just list products
	if ($cat)
	{
		$config=$db->selectObject("productsmodule_config", "location_data = '{$cat->location_data}'");
		
		$temp=unserialize($config->location_data);
		
		if ($cat->parent_id == 0) // root
		{
			// get all categories direct belong to this selected cat
			$sub_cats=productsmodule::__GetCategoryByParentID($cat->id);
			foreach ($sub_cats as $k=>$v)
			{				
				// search all sub cat of this cat
				$v->sub_cats=productsmodule::__GetCategoryByParentID($v->id);
			}
			
			exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
			// fetch to template
			$template=new template("productsmodule","_CategoriesViewParent",$loc);
			$template->register_permissions(array('administrate',"configure", "create", "edit", "delete"),$loc);
			$template->assign("cats", $sub_cats);
			$template->assign("breadcum", productsmodule::__PDoremiShop_CategorySeoNameBuild($cat->id, 1));
			$template->assign("config", $config);
			if ($cat->container_top_show == 1)
			{
				if ($cat->container_top_usediff == 1)
					$template->assign("src_top", $temp->src."_{$cat->id}_top");
				else
					$template->assign("src_top", $temp->src."_top");
			}
			else
				$template->assign("src_top", "");
				
			if ($cat->container_bottom_show == 1)
				$template->assign("src_bottom", $temp->src."_{$cat->id}_bottom");
			else
				$template->assign("src_bottom", "");
				
			$template->assign("selected_cat", $cat);
			
			// set page title
			_ab_theme_action_register_title($cat->title);
			_ab_theme_action_register_title($cat->search_des,1,0);
			
			$template->output();
		}
		else
		{ // show products on category
			// list of all  products in this category and sub categories
			// build sql_where to get all product recursive
			$sql_where="";
			$sql_where=productsmodule::__CategoriesBuildConditionRecursive($sql_where,$cat->id);
						
			$sub_cats=productsmodule::__GetCategoryByParentID($cat->id);
			
			// for the condition
			$lang=exponent_lang_loadFile("modules/productsmodule/common.php");
			// product state
			$product_state=array();
			for ($i=0;$i<$lang['product_state_count'];$i++)
			{
				$product_state[$i]=$lang["product_state{$i}"];
			}

			// product state2
			$product_state2=array();
			for ($i=0;$i<$lang['product_state_2_count'];$i++)
			{
				$product_state2[$i]=$lang["product_state_2_{$i}"];
			}
			
			// sort by
			$sort_by=array();
			for ($i=0;$i<$lang['pv_sort_by_count'];$i++)
			{
				$sort_by[$i]=$lang["pv_sort_by{$i}"];
			}
			
			exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
			
			// fetch to template
			$template=new template("productsmodule","_CategoriesViewProduct",$loc);
			$template->register_permissions(array('administrate',"configure", "create", "edit", "delete"),$loc);
			if ($cat->container_top_show == 1)
			{
				if ($cat->container_top_usediff == 1)
					$template->assign("src_top", $temp->src."_{$cat->id}_top");
				else
					$template->assign("src_top", $temp->src."_top");
			}
			else
				$template->assign("src_top", "");
				
			if ($cat->container_bottom_show == 1)
				$template->assign("src_bottom", $temp->src."_{$cat->id}_bottom");
			else
				$template->assign("src_bottom", "");
			$template->assign("config", $config);
			
			$template->assign("breadcum", productsmodule::__PDoremiShop_CategorySeoNameBuild($cat->id, 1));
			
			// product state
			$template->assign("product_states", $product_state);
			$template->assign("product_states2", $product_state2);
			$template->assign("sort_by", $sort_by);
			
			$currency=productsmodule::_GetCurrencyDefault($config->currency_id);
			// pricelist
			$price_list=productsmodule::__Generate_Price_Filter_list($currency);
			
			// for product list
			$template->assign("brands", productsmodule::__GetBrandList());
			$template->assign("selected_cat", $cat);
			$template->assign("sub_cats", $sub_cats);
			$template->assign("price_list", $price_list);
			
			$config_preprocess="__abpv_config.cat_seo_name='{$cat->seo_name}';";
			$template->assign("config_preprocess", $config_preprocess);
			// for chaining to product_getajax
			// set the cat_seo_name to target category
			$_REQUEST['cat_seo_name']=$cat->seo_name;
			// product list will be chained in template
			
			// set page title
			_ab_theme_action_register_title($cat->title);
			_ab_theme_action_register_title($cat->search_des,1,0);
			
			
			$template->output();
		}
	}
	else
		echo SITE_404_HTML;
?>
